package com.yxhl.zoume.common.ui.custom;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

import com.yxhl.zoume.R;


/**
 * Created by Xboy on 11/5/15.
 */
public class QuicklyClearEditText extends EditText {

    private Drawable drawable;

    public QuicklyClearEditText(Context context) {
        super(context);
        init(context, null);
    }

    public QuicklyClearEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    //Ethan modify 16.07.25
//    public QuicklyClearEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
//        super(context, attrs, defStyleAttr, defStyleRes);
//        init(context, attrs);
//    }


    public QuicklyClearEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(final Context context, AttributeSet attrs) {
        // 添加清除按钮
        int deleteRes = R.drawable.icon_remove;
        drawable = getResources().getDrawable(deleteRes);

        int width = (int) (drawable.getIntrinsicWidth() * 0.6);
        int height = (int) (drawable.getIntrinsicHeight() * 0.6);
        drawable.setBounds(0, 0, width, height);
        addTextChangedListener(new SimpleEditWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                displayDelete(hasFocus() && s.length() > 0);
            }
        });

        setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    String content = getText().toString();
                    if (0 < content.length()) {
                        displayDelete(true);
                    }
                } else {
                    displayDelete(false);
                }
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {

            Drawable delDrawable = getCompoundDrawables()[2];
            if (delDrawable != null) {
                // 计算触摸区域为‘删除图标’
                boolean touchable = (event.getX() > (getWidth() - getTotalPaddingRight())) &&
                        (event.getX() < (getWidth() - getPaddingRight()));

                if (touchable) {
                    this.setText("");
                }
            }
        }
        return super.onTouchEvent(event);
    }

    private void displayDelete(boolean display) {
        if (display) {
            setDrawableRight(drawable);
        } else {
            setDrawableRight(null);
        }
    }

    private void setDrawableRight(Drawable drawable) {
        setCompoundDrawables(null, null, drawable, null);
    }
}
